package com.xjrsoft.module.ureport.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.ureport.dto.GetRptFilePageListDto;
import com.xjrsoft.module.ureport.dto.RptRelationDto;
import com.xjrsoft.module.ureport.entity.XjrRptFile;
import com.xjrsoft.module.ureport.mapper.XjrRptFileMapper;
import com.xjrsoft.module.ureport.service.IXjrRptFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-12-11
 */
@Service
public class XjrRptFileServiceImpl extends ServiceImpl<XjrRptFileMapper, XjrRptFile> implements IXjrRptFileService {

    @Autowired
    private IXjrBaseModuleService xjrBaseModuleService;

    @Override
    public XjrRptFile getByName(String correctName) {
        QueryWrapper<XjrRptFile> queryWrapper = new QueryWrapper<>();
        return baseMapper.selectOne(queryWrapper.lambda().eq(XjrRptFile::getName, correctName));
    }

    @Override
    public void deleteByName(String correctName) {
        XjrRptFile xjrRptFile = getByName(correctName);
        if (xjrRptFile != null) {
            baseMapper.deleteById(xjrRptFile.getId());
        }
    }

    @Override
    public List<XjrRptFile> listAll(GetRptFilePageListDto dto) {
        QueryWrapper<XjrRptFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(dto.getKeyword()), XjrRptFile::getName, dto.getKeyword());
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean saveRptFile(RptRelationDto dto) {
        XjrRptFile xjrRptFile = BeanUtil.copy(dto, XjrRptFile.class);
        boolean saveFlag = baseMapper.insert(xjrRptFile) > 0;
        // 保存菜单
        XjrBaseModule module = BeanUtil.copy(dto, XjrBaseModule.class);
//        String url = "logic/ReportModule/ReportRelease/previewRptFile.htm?rptFileId=" + folderId;
//        module.setUrlAddress(url);
        module.setTarget("iframe");
        module.setIsMenu(1);
        module.setDeleteMark(0);
        module.setEnabledMark(1);
        boolean moduleSave = xjrBaseModuleService.save(module);
        if (saveFlag && moduleSave) {
            return true;
        } else {
            return false;
        }
    }
}
